﻿var times = true;
var xmlHttpRequest = null;
function showIPTest(ip) {
    var div = document.createElement("div");
    div.style.position = 'fixed';
    div.style.top = "0px";
    div.style.left = "5px";
    div.style.fontSize = "18px";
    div.style.padding = "0px 10px 0px 10px";
    div.style.zIndex = 999999;
    div.style.backgroundColor = '#808080';
    div.style.border = "1px solid #666";
    div.style.opacity = 0.5;
    div.style.borderRadius = "5px";

    div.innerHTML = ip;
    if (ip == "" || ip == null) return;
    if (times) {
        document.body.appendChild(div);
        times = false;
    }
}

function writeLog(msg) {
    //console.log(msg);
}
//XmlHttpRequest对象    
function createXmlHttpRequest() {
    if (window.ActiveXObject) { //如果是IE浏览器    
        return new ActiveXObject("Microsoft.XMLHTTP");
    } else if (window.XMLHttpRequest) { //非IE浏览器    
        return new XMLHttpRequest();
    }
}

function getLocalIPAddr() {
    try {
        writeLog("发送请求");
        xmlHttpRequest = createXmlHttpRequest();
        //2.设置回调函数    
        xmlHttpRequest.onreadystatechange = getIP;

        //3.初始化XMLHttpRequest组建    
        xmlHttpRequest.open("GET", "http://myapitest.ymatou.cn/Home/GetIP?url=" + window.location.href, true);

        //4.发送请求    
        xmlHttpRequest.send(null);
    } catch (e) {
        writeLog("请求错误");
    }
}

function getIP() {
    if (times)
        showIPTest(xmlHttpRequest.response);
}

if (window.location.host.indexOf(".ymatou.") > 0) {
    if (times)
        getLocalIPAddr();
}
